package com.aistar.controller;

import com.aistar.baidu_ocr.DrivingLicense;
import com.aistar.baidu_ocr.IdCard;
import com.aistar.util.JsonToDate;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

@Controller
public class OCRController {

   /* @RequestMapping("/abc")
    @ResponseBody
    public String iDCardOCR(){
        System.out.println("5555");
        return "infoFromIdCard";
    }*/

    @PostMapping("/idCardOCR")
    @ResponseBody
    public ModelAndView iDCardOCR(MultipartFile idCard){
        ModelAndView modelAndView = new ModelAndView("customer/ocr");
        byte[] bytes = null;
        try {
             bytes = idCard.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String infoFromIdCard = IdCard.getInfoFromIdCard(bytes);
        Map<String,String> map = JsonToDate.getHashMapByJson(infoFromIdCard);
        System.out.println("map"+map);
        /*String name = map.get("name");
        String nation = map.get("nation");
        String sex = map.get("sex");
        String Birth = map.get("Birth");
        String address = map.get("address");
        String IDCard = map.get("IDCard");*/

        /*System.out.println("name:"+name);
        System.out.println("nation:"+nation);
        System.out.println("sex:"+sex);
        System.out.println("Birth:"+Birth);
        System.out.println("address:"+address);
        System.out.println("IDCard:"+IDCard);*/

        modelAndView.addObject("map",map);
       /* modelAndView.addObject("name",name);
        modelAndView.addObject("sex",sex);*/


        return modelAndView;
    }


    @PostMapping("/drivingCardOCR")
    @ResponseBody
    public JSONObject drivingCardOCR(MultipartFile idCard){
        byte[] bytes = null;
        try {
            bytes = idCard.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String drivingLicense = DrivingLicense.drivingLicense(bytes);
        return JSONObject.parseObject(drivingLicense);
    }

}
